<?php

namespace App\Models\Goods;

use Illuminate\Database\Eloquent\Model;

class GoodsComment extends Model
{

    protected $table = 'goods_comments';

    protected $primaryKey = 'commentsId';

    public $timestamps = false;

    public $incrementing = false;


    /**
     * 查询商品评价数量（各个状态）
     */
    public static function getCommentCount( $mainPostID, $pcustID='' ){

        //传参数组
        $return_array = array(
            'total_count'  => 0,  //全部评价数量
            'good_count' => 0,  //好评数量
            'mid_count'   => 0,  //中评数量
            'bad_count'   => 0   //差评数量
        );

        $sql_str = [];
        if (ebsig_is_int($pcustID)) {
            $sql_str = ['display' => 0, 'pcustID' => $pcustID];
        }


        //查询商品全部评论数
        $total = GoodsComment::where(['mainPostID' => $mainPostID, 'commentStatus' => 1])
            ->where(['display' => 1])
            ->orWhere($sql_str)
            ->count();
        if ($total) {
            $return_array['total_count'] = $total;
        }

        //查询商品所有好评论数
        $good_count = GoodsComment::where([
            ['mainPostID', '=', $mainPostID],
            ['commentStatus', '=', 1],
            ['commentLevel', '>=', 4],
            ['commentLevel', '<=', 5]
        ])->where(['display' => 1])->orWhere($sql_str)->count();
        if ($good_count) {
            $return_array['good_count'] = $good_count;
        }

        //查询商品所有中评论数
        $mid_count = GoodsComment::where([
            ['mainPostID', '=', $mainPostID],
            ['commentStatus', '=', 1],
            ['commentLevel', '>=', 2],
            ['commentLevel', '<=', 3]
        ])->where(['display' => 1])->orWhere($sql_str)->count();
        if ($mid_count) {
            $return_array['mid_count'] = $mid_count;
        }

        //查询商品所有差评论数
        $bad_count = GoodsComment::where([
            ['mainPostID', '=', $mainPostID],
            ['commentStatus', '=', 1],
            ['commentLevel', '=', 1]
        ])->where(['display' => 1])->orWhere($sql_str)->count();
        if ($bad_count) {
            $return_array['bad_count'] = $bad_count;
        }

        return $return_array;
    }

}
